#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/3 15:10
# @Author : 吴敬超
# explain:
import pandas as pd

import numpy as np
import netCDF4 as nc
import datetime
import csv

file_path = 'D:\NetCDFfile\ECMWF_mrt_20190101_v1.0_con.nc'
file_obj = nc.Dataset(file_path)
# file_obj


# odict_keys = file_obj.variables.keys()
# print odict_keys
# print len(odict_keys)
# print odict_keys[0]

mrt = file_obj.variables['mrt'][:]
lon = file_obj.variables['lon'][:]
lat = file_obj.variables['lat'][:]
time = file_obj.variables['time']
#
# for j in range(len(lon)):
#     print j
#
# for i in range(len(time)):
#     print i
#
# print time[:50]

times = nc.num2date(time[:], time.units)
#
# np_data = np.asarray(mrt)
#
# # print times[:12]
# precip_ts = pd.Series(np_data, index=times)
#
# precip_ts.to_csv('precip.csv',index=True, header=True)

#
#
# for timets in range(len(times)):
#     for lata in range(len(lat)):
#         for lono in range(len(lon)):
#
#             print "time num is" + str(timets)
#             print "time :" + times[timets].strftime("%Y-%m-%d %H:%M:%S")
#
#
#             print "lat num is" + str(lata)
#             print "lat :" + str(lat[lata])
#
#             print "lon num is" + str(lono)
#             print "lon :" + str(lon[lono])

# print "mrt :" + mrt[timets][lata][lono]
columnsTitles = ['time', 'lat', 'lon', 'mrt']




for lata in range(10):

    listtime = []
    listlon = []
    listlat = []
    listmrt = []

    for lono in range(len(lon)):
        listtime.append(times[0].strftime("%Y-%m-%d %H:%M:%S"))
        listlat.append(lat[lata])
        listlon.append(lon[lono])
        listmrt.append(mrt[0][lata][lono])

        # print "999999"




    print "listtime len"

    print len(listtime)
    print "listlon len"
    print len(listlon)
    print "listlat len"
    print len(listlat)
    print "listmrt len"
    print len(listmrt)

    print "run time num"+str(lata)

    data3 = {'time': listtime, 'lat': listlat, 'lon': listlon, 'mrt': listmrt}
    df3 = pd.DataFrame(data3)

    df3.to_csv('ncmrt.csv', index=None, mode='ab', header=False,columns=columnsTitles)
